import { NRate } from "naive-ui";
import type { RateProps } from "naive-ui";
import type { HTMLAttributes } from "vue";
import { getSlotsDom } from "../utils";
import type { BaseComponentType } from "../types";
export type RateFactoryConstructorType = {
  props?: RateProps;
  attrs?: HTMLAttributes;
  defaultSlot?: (v?: { index: number }) => BaseComponentType;
};
export class RateFactory {
  private props: RateProps = {};
  private attrs: HTMLAttributes = {};
  private defaultSlot: (v?: { index: number }) => BaseComponentType = () =>
    null;

  constructor(data?: RateFactoryConstructorType) {
    if (data?.props) this.setProps(data.props);
    if (data?.attrs) this.setAttrs(data.attrs);
    if (data?.defaultSlot) this.setDefault(data.defaultSlot);
  }
  setProps(props: RateProps) {
    this.props = props;
    return this;
  }

  setAttrs(attrs: HTMLAttributes) {
    this.attrs = attrs;
    return this;
  }

  setDefault(component: (v?: { index: number }) => BaseComponentType) {
    this.defaultSlot = component;
    return this;
  }

  create() {
    return (
      <NRate {...this.attrs} {...this.props}>
        {{
          default:
            this.defaultSlot() != null
              ? (v: { index: number }) => getSlotsDom(this.defaultSlot(v))
              : null,
        }}
      </NRate>
    );
  }
}
